package com.liuweiheng.cloud.thread;

import java.util.concurrent.Callable;

/**
 * 线程状态
 * New | Runnable | Blocked | Waiting | Timed_Waiting | Terminated
 * @author lwh
 * @since 2021/5/11
 */
public class MyThreadState{

    public static void main(String[] args) {
        Thread a = new Thread(() -> {
            testMethod();
        }, "a");
        Thread b = new Thread(() -> {
            testMethod();
        }, "b");

        a.start();
        b.start();

        // main线程等待1000毫秒
        // 不等待的话，若CPU性能过高，可能会先进行打印，启动线程还未执行run方法
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // TIME_WAITING
        System.out.println(a.getName() + ":" + a.getState());
        // BLOCKED
        System.out.println(b.getName() + ":" + b.getState());

    }

    private static synchronized void testMethod() {
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
